<template>
    <Row>
        <Col span = '8'>
    
    <Form ref="formInline" :model="formInline">
        <FormItem prop="password">
            <Input type="password" v-model="formInline.pwd" placeholder="旧密码">
                <Icon type="ios-lock-outline" slot="prepend"></Icon>
            </Input>
        </FormItem>
        <FormItem prop="repassword">
            <Input type="password" v-model="formInline.repwd" placeholder="新密码">
                <Icon type="ios-lock-outline" slot="prepend"></Icon>
            </Input>
        </FormItem>
        <FormItem style="text-align:center;">
            <Button type="primary" @click="handleSubmit('formInline')">保存</Button>
        </FormItem>
    </Form>
    </Col>
    </Row>
</template>
<script>
    import { modifyPassword } from '@/api/data'
    import { getAuthkey } from '@/libs/util'
    import { mapActions, mapMutations } from 'vuex'
    export default {
        data () {
            return {
                formInline: {
                    pwd: '',
                    repwd: ''
                }
            }
        },
        methods: {
            ...mapActions([
                'handleLogOut'
            ]),
            handleSubmit(name) {
                if(this.formInline.pwd === ''){
                    this.$Message.info('请输入旧密码')
                    return
                }
                if(this.formInline.repwd === ''){
                    this.$Message.info('请输入新密码')
                    return
                }
                let data = {
                    auth_key: getAuthkey(),
                    old_pwd: this.formInline.pwd,
                    new_pwd: this.formInline.repwd
                }
                modifyPassword(data).then(res => {
                    if(res.data.code !== 200){
                        this.$Message.error(res.data.error)
                        return
                    }
                    this.$Message.success('密码修改成功')
                    this.handleLogOut().then(() => {
                        this.$router.push({
                        name: 'login'
                        })
                    })
                }, err => {
                    this.$Message.error(err?err: '修改密码失败')
                })
            }
        }
    }
</script>